/*
 * @Description: 
 * @Author: your name
 * @version: 
 * @Date: 2024-01-29 15:15:02
 * @LastEditors: your name
 * @LastEditTime: 2024-01-29 16:04:08
 */


import * as Cesium from 'cesium';
/**
 * @description: 波纹扩散圆材质
 * @duration duration
 * @count count
 * @return {*}
 */
// 支持传入颜色color，持续时间duration，扩散数量count
class CircleWaveMaterialProperty {
    constructor(options) {
        options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
        this._definitionChanged = new Cesium.Event();
        this._color = undefined;
        this._colorSubscription = undefined;
        this.color = options.color;
        this.duration = Cesium.defaultValue(options.duration, 1e3);
        this.count = Cesium.defaultValue(options.count, 2);
        if (this.count <= 0) this.count = 1;
    }
}
Object.defineProperties(CircleWaveMaterialProperty.prototype, {
    isConstant: {
        get: function () {
            return false;
        }
    },
    definitionChanged: {
        get: function () {
            return this._definitionChanged;
        }
    },
    color: Cesium.createPropertyDescriptor('color')
});
CircleWaveMaterialProperty.prototype.getType = function (time) {
    return Cesium.Material.CircleWaveMaterialType;
};
CircleWaveMaterialProperty.prototype.getValue = function (time, result) {
    if (!Cesium.defined(result)) {
        result = {};
    }
    result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
    result.count = this.count;
    result.duration=this.duration
    return result;
};
CircleWaveMaterialProperty.prototype.equals = function (other) {
    return this === other ||
        (other instanceof CircleWaveMaterialProperty &&
            Cesium.Property.equals(this._color, other._color));
};
Cesium.Material.CircleWaveMaterialType = 'CircleWaveMaterial';

Cesium.Material.CircleWaveSource =
    /*glsl*/`
    // 圆形sdf 传入uv以及半径
float sdCircle(vec2 p,float r)
{
    return length(p)-r;
}
czm_material czm_getMaterial(czm_materialInput materialInput) {
    czm_material material = czm_getDefaultMaterial(materialInput);
    float iTime=czm_frameNumber/(120.*duration/1000.);
    vec2 uv=materialInput.st;
    // 让uv的取值到-1，1之间
    uv=(uv-0.5)*2.;
    // 使用sdf函数构造圆
    float d=sdCircle(uv,iTime*.5);
    // 使用sin函数实现重复效果，构造波纹
    d=sin(d*30.);
    // 使用smoothstep去除锯齿，平滑过渡
    float c=smoothstep(0.,.01,d);
    // 定义波纹圆颜色以及中间的颜色
    vec4 cInner=vec4(color.rgb,1.);
    // 将环上的透明度分量加上指数幂可以实现发光效果
    cInner.a=pow(d,2.);
    vec4 cOuter=vec4(0.,0.,0.,0.);
    vec4 color=mix(cInner,cOuter,c);
    material.diffuse=color.rgb;
    material.alpha=color.a;
    return material;
}
`;


Cesium.Material._materialCache.addMaterial(Cesium.Material.CircleWaveMaterialType, {
    fabric: {
        type: Cesium.Material.CircleWaveMaterialType,
        uniforms: {
            color: new Cesium.Color(1.0, 0.0, 0.0, 1.0),
            duration: 1000.,
            count: 1
        },
        source: Cesium.Material.CircleWaveSource
    },
    translucent: function (material) {
        return !0;
    }
});

export default CircleWaveMaterialProperty;